package com.gmp.rooms.controller;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Random;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.gmp.common.annotation.Log;
import com.gmp.common.core.controller.BaseController;
import com.gmp.common.core.domain.AjaxResult;
import com.gmp.common.enums.BusinessType;
import com.gmp.rooms.domain.Rooms;
import com.gmp.rooms.service.IRoomsService;
import com.gmp.common.utils.poi.ExcelUtil;
import com.gmp.common.core.page.TableDataInfo;

/**
 * 厂房信息目录Controller
 * 
 * @author Koi
 * @date 2025-07-28
 */
@RestController
@RequestMapping("/rooms/roomsinformation")
@Api(tags = "厂房信息管理")
public class RoomsController extends BaseController
{
    @Autowired
    private IRoomsService roomsService;

    /**
     * 查询厂房信息目录列表
     */
    @PreAuthorize("@ss.hasPermi('rooms:roomsinformation:list')")
    @GetMapping("/list")
    public TableDataInfo list(Rooms rooms)
    {
        startPage();
        List<Rooms> list = roomsService.selectRoomsList(rooms);
        return getDataTable(list);
    }

    /**
     * 根据Room——Code查询厂房名称
     */
    @GetMapping("/getRoomNameByRoomCode/{roomCode}")
    @ApiOperation("根据Room——Code查询厂房名称")
    public AjaxResult getRoomNameByRoomCode(@PathVariable String roomCode)
    {
        LambdaQueryWrapper<Rooms> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Rooms::getRoomCode, roomCode);
        Rooms rooms = roomsService.getOne(queryWrapper);
        return success(rooms);
    }

    /**
     * 导出厂房信息目录列表
     */
    @PreAuthorize("@ss.hasPermi('rooms:roomsinformation:export')")
    @Log(title = "厂房信息目录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Rooms rooms)
    {
        List<Rooms> list = roomsService.selectRoomsList(rooms);
        ExcelUtil<Rooms> util = new ExcelUtil<Rooms>(Rooms.class);
        util.exportExcel(response, list, "厂房信息目录数据");
    }

    /**
     * 获取厂房信息目录详细信息
     */
    @PreAuthorize("@ss.hasPermi('rooms:roomsinformation:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(roomsService.selectRoomsById(id));
    }

    /**
     * 新增厂房信息目录
     */
    @PreAuthorize("@ss.hasPermi('rooms:roomsinformation:add')")
    @Log(title = "厂房信息目录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Rooms rooms)
    {
        // 生成格式：R+日期（yyyyMMdd）+3位随机数（如 R20230728123）
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String dateStr = sdf.format(new Date());
        // 生成100-999之间的随机数
        int randomNum = new Random().nextInt(900) + 100;
        String newRoomCode = "R" + dateStr + randomNum;
        rooms.setStateEvaluation("状态评估将根据设备巡检结果自动生成");
        //设计逻辑删除
        rooms.setIsDelete(0);
        //设置建成时间
        rooms.setCreatedAt(new Date());
        rooms.setRoomCode(newRoomCode);
        return toAjax(roomsService.save( rooms));
    }

    /**
     * 修改厂房信息目录
     */
    @PreAuthorize("@ss.hasPermi('rooms:roomsinformation:edit')")
    @Log(title = "厂房信息目录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Rooms rooms)
    {
        return toAjax(roomsService.updateRooms(rooms));
    }

    /**
     * 删除厂房信息目录
     */
    @PreAuthorize("@ss.hasPermi('rooms:roomsinformation:remove')")
    @Log(title = "厂房信息目录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(roomsService.removeBatchByIds(Arrays.asList(ids)));
    }

    /**
     * 查询所有厂房数据
     */
    @PreAuthorize("@ss.hasPermi('rooms:roomsinformation:findAll')")
    @PostMapping("/findAll")
    @ApiOperation("查询所有厂房数据")
    public AjaxResult findAll()
    {
    	return success( roomsService.findAll());
    }

}
